import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost'
//引入axios
import axios from 'axios'
//给vue对象添加原型
Vue.prototype.$http = axios
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

//创建路由
const router = new VueRouter({
  routes
})

/*axios前置拦截器*/
axios.interceptors.request.use(config => {
    //携带token
    let token = localStorage.getItem("token");
    if (token) {
        //在头信息中添加token
        config.headers['token'] = token;
    }
    return config;
}, error => {
    Promise.reject(error);
});
//axios后置拦截器
axios.interceptors.response.use(result => {
    let {success,msg}=result.data;
    if(!success && msg==="noLogin"){
      //清空local
        localStorage.removeItem("token")
        localStorage.removeItem("loginUser");
        //跳转到登录界面
        router.push({ path: '/login' });
    }
    return result;
}, error => {
    Promise.reject(error);
});


router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login'||to.path=="/shop") {//如果访问的是登录或者商家入驻就放行
        next()
  }else{//访问其他资源，如果token里面有值，就放行
      let token=localStorage.getItem("token");
      if(token){
          next()
      }else{
          //跳转到登录界面
          router.push({ path: '/login' });
      }
  }

})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

